"use strict";
cc._RF.push(module, '38f44TRMUNA6bC4B7gtvl0j', 'main');
// script/main.js

'use strict';

var global = require('global');
var audioManger = require('audioManger');
var com = require('Common');
cc.Class({
	extends: cc.Component,

	properties: {
		bgMusic: { type: cc.AudioClip, default: null },
		clearMusic: { type: cc.AudioClip, default: null },
		overMusic: { type: cc.AudioClip, default: null },
		badMusic: { type: cc.AudioClip, default: null },
		gamescene: cc.Prefab,
		selectPanel: cc.Prefab,
		storyPanel: cc.Prefab,
		endlistPanel: cc.Prefab,
		panelnode: cc.Node,
		_panelpool: [], //放置预制件的节点池
		musicpool: { type: cc.AudioClip, default: [] } //放置背景音乐的节点池
	},

	onLoad: function onLoad() {
		var _this = this;

		// 播放背景音乐
		audioManger.init();
		com.soundYinliang = 0.8;
		com.effectYinliang = 0.8;
		audioManger.playerBgMusic(this.bgMusic);
		//cc.audioEngine.play(this.bgMusic, true, 1);
		//global.bgmId = cc.audioEngine.play(this.bgMusic, true, 1);
		//cc.log('main:global.bgmId='+bgid);
		//this.playBgm();

		//监听关卡选择id
		cc.director.on('qkselect', function (e) {
			var gamescene = cc.instantiate(_this.gamescene);
			gamescene.parent = _this.panelnode;
			_this._panelpool[0] = gamescene;
			cc.director.emit('gamescene', e.detail);
		}, this);

		//监听背景音乐修改
		cc.director.on('changebg', function (e) {
			cc.log('changebg on==========');
			if (e.detail.name == 'music') {
				//背景音乐
				cc.log('changebg to music');
				cc.audioEngine.stopAll();
				cc.audioEngine.play(_this.bgMusic, true, 1);
			} else if (e.detail.name == 'bad') {
				cc.log('changebg to bad');
				cc.audioEngine.stopAll();
				//cc.audioEngine.pause(global.badBgmId);
				cc.audioEngine.play(_this.badMusic, true, 1);
			} else if (e.detail.name == 'clear') {
				cc.log('changebg to clear');
				cc.audioEngine.stopAll();
				//cc.audioEngine.pause(global.clearBgmId);
				cc.audioEngine.play(_this.clearMusic, true, 1);
			} else if (e.detail.name == 'gameover') {
				cc.log('changebg to gameover');
				cc.audioEngine.stopAll();
				//cc.audioEngine.pause(global.overBgmId);
				cc.audioEngine.play(_this.overMusic, true, 1);
			}
		}, this);
	},

	//测试用修改背景音乐
	changebgm: function changebgm() {
		var r = Math.random() * 4;
		cc.log('------changebgm:r=' + r);
		if (r > 3) {
			cc.director.emit('changebg', { name: 'bad' });
		} else if (r > 2) {
			cc.director.emit('changebg', { name: 'clear' });
		} else {
			cc.director.emit('changebg', { name: 'gameover' });
		}
	},
	onBtnClick: function onBtnClick(event, customData) {
		switch (customData) {
			case 'select':
				//console.log('-=-=-=任务完成');
				if (global.gkjindu > 0) {
					var selectPanel = cc.instantiate(this.selectPanel);
					selectPanel.parent = this.panelnode;
				} else {
					//刚开始玩时直接进入起始对话id为0的gamescene
					this.gotostory();
				}
				break;
			case 'story':
				//story模式就是固定打开起始对话id为0的gamescene
				this.gotostory();
				break;
			case 'endlist':
				var endlistPanel = cc.instantiate(this.endlistPanel);
				endlistPanel.parent = this.panelnode;
				break;
			default:
				break;

		}
	},
	gotostory: function gotostory() {
		var gamescene = cc.instantiate(this.gamescene);
		gamescene.parent = this.panelnode;
		cc.director.emit('gamescene', 0);
	},
	start: function start() {
		var _this2 = this;

		//监听关卡选择id
		cc.director.on('returntomain', function (e) {
			cc.log('returntomain on==========');
			_this2.panelnode.removeAllChildren;
		}, this);
	},

	//测试用打开关卡选择面板
	selectpanel: function selectpanel() {
		var selectPanel = cc.instantiate(this.selectPanel);
		selectPanel.parent = this.panelnode;
	}
	// update (dt) {},

});

cc._RF.pop();